﻿@using Modules.Core.Blazor.Components
@using Modules.Template.Blazor.Pages.CodeTemplate.Components
@using Modules.Template.Shared.CodeTemplate
<GridRow Align="middle" Style="height:48px;">
    <GridCol Span="16">
        @if (TableDataModel.TableOptions.IsEdit)

        {
            <Button OnClick="@AddClick" Icon="@IconType.Outline.Plus" Type="primary">添加</Button>
        }
        <ObjectDataExportExcel ExportDataClick="ExportExcelClick"></ObjectDataExportExcel>
    </GridCol>

    <GridCol Span="8">
        <div class="Table_Right_Tool">
            <Badge Count="FilterCount"><Button Type="@ButtonType.Text" Size="@ButtonSize.Small" Icon="@IconType.Outline.Reload" OnClick="@ResetTable">刷新</Button></Badge>
            @if (TableDataModel.TableOptions.IsEdit)
            {
                <Button Type="@ButtonType.Text" Size="@ButtonSize.Small" Icon="@IconType.Outline.Delete" OnClick="@BatchDelClick">批量</Button>
                <Button Size="@ButtonSize.Small" Type="@ButtonType.Text" Icon="@IconType.Outline.Edit">批量</Button>
            }
            <Button Type="@ButtonType.Text" Size="@ButtonSize.Small" Icon="@IconType.Outline.Setting" OnClick="@SettingTable">设置</Button>
        </div>
    </GridCol>
</GridRow>

@if (!TableDataModel.IsValid())
{
    <div style="text-align: center; padding: 30px 50px;">
        <Spin />
    </div>
}
else
{
    <Table @ref="AntTable" RowClassName="@(x => x.Data.Row_CSS_Class)"
           ScrollY="@TableDataModel.TableOptions.ScrollY" Bordered="true"
           Size=@TableSize.Small
           Resizable
           TItem="CodeTemplateVM"
           DataSource="TableDataModel.TableModel.DataSource"
           @bind-PageIndex="TableDataModel.TableModel.PageIndex"
           @bind-PageSize="TableDataModel.TableModel.PageSize"
           @bind-SelectedRows="TableDataModel.TableModel.SelectedRows"
           Total="TableDataModel.TableModel.TotalCount"
           Loading="TableDataModel.TableModel.Loading"
           OnChange="HandleTableChange" RemoteDataSource>
        <ChildContent>
            <PropertyColumn Property="c=>c.Row_no" Width="50" Title="" />
            <Selection Type="checkbox" />
            @if (TableDataModel.TableOptions.IsEdit)
            {
                <ActionColumn Title="操作" Width="70">
                    <Space Size="@($"1")">
                        <SpaceItem>
                            <Button Size="@ButtonSize.Small" OnClick="()=>EditClick(context)" Type="@ButtonType.Link" Icon="@IconType.Outline.Edit" />
                        </SpaceItem>
                        <SpaceItem>
                            <Button Size="@ButtonSize.Small" OnClick="()=>DelClickAsync(context)" Type="@ButtonType.Link" Icon="@IconType.Outline.Delete" />
                        </SpaceItem>
                    </Space>
                </ActionColumn>

            }
            @* //这里的T 是数据库实体，调整实体顺序，可以调整显示的顺序*@
            @foreach (var column in TableDataModel.ColumnHeaderModels)
            {
                <AntDesign.Column TData="object" Hidden="@column.isHidden" OnHeaderCell="@(()=>(new Dictionary<string, object>() {{"style","background-color:" +@column.bgcolor+";" }}))" Width="@column.width.ToString()" Ellipsis="@TableDataModel.TableOptions.Ellipsis" Sortable DataIndex=@($"{column.field}")>
                    <TitleTemplate>
                        <div @onclick:stopPropagation="true">
                            <Input Placeholder="@column.title" AllowClear="true" @bind-Value="@column.search" OnChange="(e)=>OnInputClearChange(e,column.field)" TValue="string" />
                        </div>
                    </TitleTemplate>
                    @*            <ChildContent>
            @Infrastructure.Common.Helpers.ReflectionHelper.GetValue(context,column.field)
            </ChildContent>*@
                </AntDesign.Column>

            }
        </ChildContent>
        <PaginationTemplate>
            <Pagination Style="text-align:center;line-height:38px;"
                        Total="context.Total"
                        Size="PaginationSize.Small"
                        ShowTotal=showTotal
                        PageSize="context.PageSize"
                        Current="context.PageIndex"
                        OnChange="context.HandlePageChange"
                        PageSizeOptions="TableDataModel.TableOptions.PageSizeOptions" />
        </PaginationTemplate>
    </Table>
}
<Modal Title="@Dialog.Title"
       @bind-Visible="@Dialog.Visible"
       Width="@Dialog.Width"
       Draggable="@Dialog.Draggable"
       DestroyOnClose
       DragInViewport="@Dialog.DragInViewport"
       
       Footer="null">
    <CodeTemplateAdd model="@EditRow" IsAdd="@IsAdd" OnValueCallback="OnValueCallback"></CodeTemplateAdd>
</Modal>
@*用户自定义列宽度*@
<Modules.Core.Blazor.Components.UserColumnSetting UniqueID="@UniqueID" TableOptions="TableDataModel.TableOptions" ColumnHeaders="@TableDataModel.ColumnHeaderModels" userColumnDrawerVisible="@userColumnDrawerVisible" OnValueCallback="@OnUserColumnDrawerCallbackAsync"></Modules.Core.Blazor.Components.UserColumnSetting>
@code

{
    Func<PaginationTotalContext, string> showTotal = ctx => $"总计 {ctx.Total} 项";
}